//
//  GatherControlView.m
//  LiveGather
//
//  Created by macintosh on 16/9/13.
//  Copyright © 2016年 Yue Shen. All rights reserved.
//

#import "GatherControlView.h"
#import "GatherMonitor.h"
#import "MonitorInfo.h"
#import "GatherControlBar.h"
#import "UIView+YYAdd.h"
#import "UIImage+YKLiveGather.h"

@interface GatherControlView ()
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UIButton *endBtn;
@property (nonatomic, strong) UIButton *liveBtn;

@property (nonatomic, strong) GatherControlBar *controlBar;
@property (nonatomic, strong) MonitorInfo *monitorInfo;
@property (nonatomic, strong) GatherMonitor *monitor;

@property (nonatomic, assign) NSTimeInterval liveSeconds;

@end

@implementation GatherControlView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _liveSeconds = 0;
        [self setupSubviews];
        [self updateLayoutOfSubviewsWithOrentationPortrait:YES];
    }
    return self;
}

- (void)setupSubviews
{
    [self addSubview:self.timeLabel];
    [self addSubview:self.endBtn];
    [self addSubview:self.liveBtn];
    [self addSubview:self.monitor];
    
    [self.waterView class];
}

- (NSString *)timeFromSeconds:(NSTimeInterval)seconds
{
    NSString *str = nil;
    if (seconds < 0) {
        str = @"--:--";
    } else if (seconds < 60) {
        str = [NSString stringWithFormat:@"00:%02d", (int)seconds];
    } else if (seconds >= 60 && seconds < 60 * 60) {
        str = [NSString stringWithFormat:@"%02d:%02d", (int)round(seconds/60), (int)seconds%60];
    } else {
        seconds = (int)seconds % (60 * 60);
        str = [NSString stringWithFormat:@"%02d:%02d", (int)round(seconds/60), (int)seconds%60];
    }
    
    return str;
}

#pragma mark - public

- (void)updateLayoutOfSubviewsWithOrentationPortrait:(BOOL)isPortrait
{
    _timeLabel.centerX = self.width * 0.5;
    _timeLabel.top = 30;
    _endBtn.centerY = _timeLabel.centerY;
    _endBtn.right = self.width - 16;
    _monitor.top = 60;
    _monitor.left = 16;
    
    if (isPortrait) {
        _liveBtn.centerX = self.width * 0.5;
        _liveBtn.bottom = self.height - 20;
        [_controlBar updateLayoutWithFoldedFrame:CGRectMake(self.width - 16 - 44, _liveBtn.centerY - 44 * 0.5, 44, 44) isPortrait:isPortrait];
    } else {
        _liveBtn.centerY = self.height * 0.5;
        _liveBtn.right = self.width - 16;
        [_controlBar updateLayoutWithFoldedFrame:CGRectMake(_liveBtn.centerX - 44 * 0.5, self.height - 26 - 44, 44, 44) isPortrait:isPortrait];
    }
}

- (void)updateMonitorWithMonitorInfo:(MonitorInfo *)info
{
    if (info) {
        _liveSeconds ++;
        _timeLabel.text = [self timeFromSeconds:_liveSeconds];
        _monitor.monitorInfo = info;
    }
}

- (void)updateLiveBtnState:(BOOL)selected
{
    _liveBtn.selected = selected;
}

- (void)resetDisplayInfo
{
    _liveSeconds = 0;
    _timeLabel.text = @"--:--";
    _monitor.state = GatherMonitorUnknown;
    _monitor.resStr = @"----";
    _monitor.monitorInfo = [MonitorInfo monitorInfo];
}

#pragma mark - control events

- (void)endBtnClicked:(UIButton *)btn
{
    if (_endLive) {
        _endLive();
    }
}

- (void)liveBtnClicked:(UIButton *)btn
{
    btn.selected = !btn.isSelected;
    
    if (_liveBtnClicked) {
        _liveBtnClicked(btn.isSelected);
    }
}

#pragma mark - setter

- (void)setBarButtons:(NSArray *)barButtons
{
    _barButtons = barButtons;
    
    GatherControlBar *tmp = _controlBar;
    if (tmp) {
        [tmp removeFromSuperview];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
            [tmp class];
        });
    }
    
    BOOL isPortrait = self.width < self.height ? YES : NO;

    _controlBar = [GatherControlBar gatherControlBarWithButtons:barButtons frame:CGRectMake(self.width - 16 - 44, self.height - 24 - 44, 44, 44) isPortrait:isPortrait];
    [self addSubview:_controlBar];
}

- (void)setResStr:(NSString *)resStr
{
    _resStr = resStr;
    _monitor.resStr = resStr;
}

- (void)setState:(GatherMonitorState )state
{
    _state = state;
    _monitor.state = state;
}

- (void)layoutWaterViewByCanvasSize:(CGSize)canvasSize flipped:(BOOL)flipped
{
    CGSize sizeRate = CGSizeMake(canvasSize.width / self.width, canvasSize.height / self.height);
    
    _waterView.centerY = _timeLabel.centerY * sizeRate.height;
    //使用mirror没有自己处理方便，关键是mirror改变的原本的画布的方向，这样用户看起来会很别扭
    //摄像头position改变时，会翻转画布，所以需要做相应处理
    if (flipped) {
        _waterView.left = canvasSize.width - 16 * sizeRate.width -  _waterView.width;
        _waterView.image = [[UIImage imageNamed:@"live"] imageByFlipHorizontal];
    } else {
        _waterView.left = 16 * sizeRate.width;
        _waterView.image = [UIImage imageNamed:@"live"];
    }
}

#pragma mark - getter

- (UILabel *)timeLabel
{
    if (!_timeLabel) {
        _timeLabel = [UILabel new];
        _timeLabel.textColor = [UIColor whiteColor];
        _timeLabel.numberOfLines = 1;
        _timeLabel.text = @"--:--";
        _timeLabel.font = [UIFont systemFontOfSize:18];
        _timeLabel.size = CGSizeMake(50, 22);
    }
    return _timeLabel;
}

- (UIButton *)endBtn
{
    if (!_endBtn) {
        _endBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_endBtn setImage:[UIImage imageNamed:@"shut_down"] forState:UIControlStateNormal];
        [_endBtn addTarget:self action:@selector(endBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        _endBtn.size = CGSizeMake(44, 44);
    }
    return _endBtn;
}

- (UIButton *)liveBtn
{
    if (!_liveBtn) {
        _liveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_liveBtn setImage:[UIImage imageNamed:@"start"] forState:UIControlStateNormal];
        [_liveBtn setImage:[UIImage imageNamed:@"suspended"] forState:UIControlStateSelected];
        [_liveBtn addTarget:self action:@selector(liveBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        _liveBtn.size = CGSizeMake(60, 60);
    }
    return _liveBtn;
}

- (UIImageView *)waterView
{
    if (!_waterView) {
        _waterView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"live"]];
        _waterView.contentMode = UIViewContentModeScaleAspectFill;
        _waterView.size = CGSizeMake(60, 30);
    }
    return _waterView;
}

- (GatherMonitor *)monitor
{
    if (!_monitor) {
        _monitor = [GatherMonitor gatherMonitorWithGathorState:0 res:nil monitorInfo:nil];
        _monitor.size = CGSizeMake(150, 100);
    }
    return _monitor;
}

@end
